import { Component, ElementRef, OnInit, Input, Output, EventEmitter, ViewChild } from '@angular/core';
import { HttpModule } from '@angular/http';
import { HttpClientModule, HttpClient, HttpErrorResponse } from '@angular/common/http';
import { FormsModule } from '@angular/forms';
import { RouterModule, Router, ActivatedRoute } from '@angular/router';

import { Service } from '../../../server/server';
import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
import { HintsService } from '../../../public/serve/hints/hints.service';
declare var $: any;

@Component({
  selector: 'app-roles-add',
  templateUrl: './roles-add.component.html',
})

export class RolesAddComponent implements OnInit {

  constructor(
    private http: HttpClient, 
    private router: Router, 
    private server: Service, 
    private cudTableService: CudTableService,
    private hintsService:HintsService
    ) { }

  createUserId: any;
  //datatype
  rolesadd: rolesaddtype = { roleGroupName: '', description: '', createUserId: '' };
  ngOnInit() {
    this.createUserId = this.server.getuser();
    //console.log(this.createUserId)
  }
  //清空
  @ViewChild('newlyAddedForm') formElement: HTMLFormElement

  reset() {
    this.formElement.reset();
    $("#add").modal("hide");
  }

  @Output()
  addOutput: EventEmitter<any> = new EventEmitter();

  public publicBtn:boolean = true;
  submitAdd() {
    this.rolesadd.createUserId = this.createUserId;
    let url = this.server.baseUrl+'admin/roleGroups/add';
    let data: rolesaddtype = this.rolesadd;
    //console.log(data)
    this.cudTableService.postRequest(url, data).then((res: any) => {
      this.addOutput.emit(res.result_code);
      //console.log(res.result_code)
      this.hintsService.messages(res.message,res.result_code);
      if (res.result_code == '0000') {
          this.reset();
      } 
    }).catch()
  }
}
export class rolesaddtype {
  roleGroupName: string;
  description: string;
  createUserId: string;
}
